import axios from 'axios';
import { closeToast, showLoadingToast } from 'vant';

export default class Request {
  static async get(url: string, params?: any, bLoading = true) {
    console.log("[axios]get", url, params);
    if (params === undefined) {
      params = {};
    }
    if (bLoading) {
      showLoadingToast({ message: '加载中...', forbidClick: true })
    }
    const res = await axios.get(url, params)
    if (bLoading) { closeToast(true) }
    console.log("[axios]resp", url, res?.data);
    return res?.data
  }
  static async post(url: string, data?: any, config?: any) {
    console.log("[axios]post", url, data, config);
    if (data === undefined) {
      data = {};
    }
    showLoadingToast({ message: '加载中...', forbidClick: true })
    const res = await axios.post(url, data, config)
    closeToast(true)
    console.log("[axios]resp", url, res?.data);
    return res?.data
  }
}

axios.defaults.baseURL = '/api'
axios.defaults.withCredentials = true
axios.defaults.timeout = 10000
axios.interceptors.request.use(
  config => {
    return config
  },
  err => {
    return Promise.reject(err)
  }
)

axios.interceptors.response.use(
  response => {
    return response
  },
  error => {
    return Promise.reject(error.response) // 返回接口返回的错误信息
  }
)

export {
  axios
}